package com.jkt.rms.businessservice.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;

import com.jkt.rms.business.Project;
import com.jkt.rms.business.ResourceRole;
import com.jkt.rms.business.SystemRole;
import com.jkt.rms.businessservice.ResourceBusinessService;
import com.jkt.rms.cache.RMSCache;
import com.jkt.rms.commons.utils.EqualityUtils;
import com.jkt.rms.commons.utils.RMSException;
import com.jkt.rms.commons.utils.ServiceLocator;
import com.jkt.rms.constants.IConstants;
import com.jkt.rms.dataservice.ResourceDataService;
import com.jkt.rms.vo.OptionVO;
import com.jkt.rms.vo.ProjectVO;
import com.jkt.rms.vo.ResourceRoleVO;
import com.jkt.rms.vo.SystemRoleVO;

public class ResourceBusinessServiceImpl implements ResourceBusinessService {
	private ResourceDataService resourceDataService;
	private RMSCache cache;

	private static final Logger LOG = Logger.getLogger(ResourceBusinessServiceImpl.class);
	
	public void setResourceDataService(ResourceDataService resourceDataService) {
		this.resourceDataService = resourceDataService;
	}
	
	@Override
	public List<ResourceRoleVO> findProjectRole() throws RMSException {
		getCache(); 
		List<ResourceRole> listResourceRole =  cache.getAllResourceRoles();
		return getResourceRoleVOList(listResourceRole);
		
	}
	
	private List<ResourceRoleVO> getResourceRoleVOList(List<ResourceRole> projectRoleList) {
		final List<ResourceRoleVO> resourceRoleVOList = new ArrayList<ResourceRoleVO>();
		for (ResourceRole projectRole : projectRoleList) {
			try {
				resourceRoleVOList.add(getResourceRoleVO(projectRole));
			} catch (RMSException e) {
				LOG.error(" Exception occured in search", e.getCause());
			}
		}
		return resourceRoleVOList;
	}
	
	private ResourceRoleVO getResourceRoleVO(ResourceRole projectRoleData) throws RMSException {
		final ResourceRoleVO projectVO = new ResourceRoleVO();
		EqualityUtils.copyProperties(projectVO, projectRoleData);
		return projectVO;
	}
	
	@Override
	public List<OptionVO<String>> findVendor() throws RMSException {
		getCache();
		return cache.getAllVendorOptions();
		
	}
	
	@Override
	public List<OptionVO<Integer>> findLocation() throws RMSException {
		getCache();
		return cache.getAllLocationOptions();
		
	}
	
	@Override
	public List<ProjectVO> findAllActiveProject() throws RMSException {
		getCache();
		return getProjectVOList(cache.getAllActiveProject());
		
	}
	
	private List<ProjectVO> getProjectVOList(List<Project> projectList) {
		
		ProjectVO projectVO = null;
		List<ProjectVO> projectlistVO = new ArrayList<ProjectVO>();
			for (Project project : projectList) {
				projectVO = new ProjectVO();
				projectVO.setProjectNumber(project.getProjectNumber());
				projectVO.setProjectName(project.getProjectName());
				projectlistVO.add(projectVO);
			}
		return projectlistVO;
	}
	
	private ProjectVO getProjectVO(Project project) throws RMSException {
		final ProjectVO projectVO = new ProjectVO();
		BeanUtils.copyProperties(project, projectVO);
		return projectVO;
	}
	

	private RMSCache getCache() {
		cache = ServiceLocator.getServiceLocator().locate("cacheService");
		return cache;
	}
	 
}
